package ups.glre.news.jms;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;

@MessageDriven(activationConfig = {
		@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
		@ActivationConfigProperty(propertyName = "destination", propertyValue = "topic/newsTopic"),
		@ActivationConfigProperty(propertyName = "messageSelector",
				propertyValue =
					Constants.NEWS_CATEGORY_PROPERTY_NAME + " = 'user' OR " +
					Constants.NEWS_CATEGORY_PROPERTY_NAME + " = 'subcribe' OR " +
					Constants.NEWS_CATEGORY_PROPERTY_NAME + " = 'docs' OR " +
					Constants.NEWS_CATEGORY_PROPERTY_NAME + " = 'calendar' OR " +
					Constants.NEWS_CATEGORY_PROPERTY_NAME + " = 'news'")} )
public class NewsBean implements MessageListener {
	private static final Logger log = Logger
			.getLogger(NewsBean.class.getName());

	@Override
	public void onMessage(Message msg) {
		try {

			NewsMessage news = getNewsMesage(msg);
			log.info("Message received of type: " + news);
		} catch (JMSException ex) {
			log.log(Level.SEVERE, "Error receiving message", ex);
		}

	}

	private NewsMessage getNewsMesage(Message msg) throws JMSException {
		NewsMessage newsMessage = new NewsMessage();
		
		newsMessage.setCategory(msg.getStringProperty(Constants.NEWS_CATEGORY_PROPERTY_NAME));
		newsMessage.setAuthor(msg.getStringProperty(Constants.NEWS_AUTHOR_PROPERTY_NAME));
		newsMessage.setContent(msg.getStringProperty(Constants.NEWS_CONTENT_PROPERTY_NAME));
		newsMessage.setDate(msg.getStringProperty(Constants.NEWS_DATE_PROPERTY_NAME));
		newsMessage.setTitle(msg.getStringProperty(Constants.NEWS_TITLE_PROPERTY_NAME));

		return newsMessage;
	}
}
